package com.controller;

import com.entity.appInfo;
import com.entity.appVersion;
import com.entity.backendUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.backManageService;
import com.service.developerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class backManageController {
    @Autowired
    backManageService backManageService;
    @Autowired
    developerService developerService;

    /**
     * 根据用户名和密码查询后台管理者信息
     */
    @RequestMapping("/backManagementLogin")
    public ModelAndView backManageLogin(String userCode, String userPassword, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        backendUser backendUser = backManageService.backManageLogin(userCode, userPassword);
        String mess;
        if (backendUser != null){
            request.getSession().setAttribute("backendUser",backendUser);
            modelAndView.addObject("backendUser",backendUser);
            modelAndView.setViewName("mainBackManagement");
        }else {
            mess="用户名或密码错误";
            modelAndView.addObject("mess",mess);
            modelAndView.setViewName("loginBackManagement");
        }
        return modelAndView;
    }

    /**
     * 用户退出
     */
    @RequestMapping("/signOutBack")
    public ModelAndView signOut(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        request.getSession().invalidate();
        modelAndView.setViewName("/loginBackManagement");
        return modelAndView;
    }

    /**
     * 分页展示app内容，并可以根据条件查询
     */
    @RequestMapping("/backManageShow")
    public ModelAndView devShow(String softwareName,String flatForm, Integer categoryName1, Integer categoryName2,
                                Integer categoryName3, @RequestParam(defaultValue = "1", value = "pageIndex") Integer pageIndex) {
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(pageIndex, 5);
        List<appInfo> appInfoList = backManageService.backManageShow(softwareName, flatForm, categoryName1, categoryName2, categoryName3);
        PageInfo<appInfo> pageInfo = new PageInfo<>(appInfoList);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.addObject("appInfoList", appInfoList);
        modelAndView.addObject("softwareName",softwareName);
        modelAndView.addObject("flatForm",flatForm);
        modelAndView.addObject("categoryName1",categoryName1);
        modelAndView.addObject("categoryName2",categoryName2);
        modelAndView.addObject("categoryName3",categoryName3);
        modelAndView.setViewName("/appReview");
        return modelAndView;
    }

    /**
     * 查询有没有最新版本号
     */
    @RequestMapping("/latestVersion")
    @ResponseBody
    public int latestVersion(Integer appId){
        int i;
        if (appId != null){
            i = backManageService.latestVersion(appId);
        }else {
            i= -1;
        }
        return i;
    }

    /**
     * 审核app，展示app基本内容弄与最新版本号
     */
    @RequestMapping("/reviewAppShow")
    public ModelAndView reviewAppShow(Integer appId){
        ModelAndView modelAndView = new ModelAndView();
//        获取app基本信息
        appInfo appInfo = developerService.modifyFindApp(appId);
        modelAndView.addObject("appInfo",appInfo);
//        获取最新版本信息
        appVersion appVersion = developerService.findAppVersion(appId);
        modelAndView.addObject("appVersion",appVersion);
        modelAndView.addObject("appId",appId);
        modelAndView.setViewName("review");
        return modelAndView;
    }

    /**
     * 审核app通过或者不通过
     */
    @RequestMapping("/reviewAdoptOrFail")
    public ModelAndView reviewAdoptOrFail(Integer status,Integer appId) {
        ModelAndView modelAndView = new ModelAndView();
        String mess;
        if (appId == null) {
            mess = "未获取到该app的信息，请刷新后再次尝试";
            modelAndView.addObject("mess", mess);
            modelAndView.setViewName("review");
        } else{
            backManageService.reviewAdoptOrFail(status, appId);
            modelAndView.setViewName("redirect:/backManageShow");
        }
        return modelAndView;
    }
}
